package liuwanghui.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import liuwanghui.common.Result;
import liuwanghui.service.DealerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import liuwanghui.dto.request.ImportOrdersDTO;
import liuwanghui.dto.request.ProductQueryDTO;
import liuwanghui.dto.request.ProductStatusDTO;

@RestController
@RequestMapping("/dealer")
@RequiredArgsConstructor
@CrossOrigin
@Tag(name = "经销商", description = "经销商相关接口")
public class DealerController {

    private final DealerService dealerService;

    @GetMapping("/statistics")
    @Operation(summary = "获取统计数据")
    public Result getStatistics(
            @RequestParam(required = false, defaultValue = "month") String timeRange,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String category,
            @RequestParam(required = false, defaultValue = "false") Boolean includeDetails) {
        return dealerService.getStatistics(timeRange, startDate, endDate, category, includeDetails);
    }

    @GetMapping("/inventory/low-stock")
    @Operation(summary = "获取库存预警商品")
    public Result getLowStockProducts(
            @RequestParam(required = false, defaultValue = "10") Integer threshold,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String warehouse,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return dealerService.getLowStockProducts(threshold, category, warehouse, page, pageSize);
    }

    @PostMapping("/inventory/import-orders")
    @Operation(summary = "订单入库")
    public Result importOrdersToStock(@RequestBody @Valid ImportOrdersDTO dto) {
        return dealerService.importOrdersToStock(dto);
    }

    @GetMapping("/products")
    @Operation(summary = "获取商品列表")
    public Result getProducts(ProductQueryDTO params) {
        return dealerService.getProducts(params);
    }

    @PutMapping("/products/{productId}/status")
    @Operation(summary = "更新商品状态")
    public Result updateProductStatus(
            @PathVariable Long productId,
            @RequestBody @Valid ProductStatusDTO dto) {
        return dealerService.updateProductStatus(productId, dto);
    }

    @DeleteMapping("/products/{id}")
    @Operation(summary = "删除商品")
    public Result deleteProduct(@PathVariable Long id) {
        return dealerService.deleteProduct(id);
    }

    @GetMapping("/products/hot")
    @Operation(summary = "获取热销商品")
    public Result getHotProducts() {
        return dealerService.getHotProducts();
    }

    @GetMapping("/orders/recent")
    @Operation(summary = "获取最近订单")
    public Result getRecentOrders() {
        return dealerService.getRecentOrders();
    }

    @GetMapping("/orders/completed")
    @Operation(summary = "获取已完成订单")
    public Result getCompletedOrders() {
        return dealerService.getCompletedOrders();
    }

    @GetMapping("/inventory/products")
    @Operation(summary = "获取库存商品")
    public Result getInventoryProducts() {
        return dealerService.getInventoryProducts();
    }
}